<template>
  <el-dialog title="需要顾客填写的字段" :visible.sync="dialogVisible" :close-on-click-modal="false" width="750px" top="5vh">
    <!-- 说明 -->
    <div class="app-main-tips-box">
      <div class="title">
        <i class="el-icon-warning" /> 说明
      </div>
      <ul>
        <li>1.姓名，手机号为必选。</li>
        <li>2.除了姓名和手机号，你最多可以选择3项是必填的。</li>
      </ul>
    </div>

    <!-- 同步卡包填写字段 -->
    <el-form ref="form" :rules="rules" size="small" label-width="100px">
      <el-form-item v-for="(item,index) in fields" :key="index" :label="item.fieldName" prop="code">
        <el-switch v-model="switchForm[index]" :disabled="item.id===1||item.id===2" />&nbsp;&nbsp;
        <span v-if="item.id===1||item.id===2">必填</span>
        <el-checkbox v-else v-model="checkboxForm" :label="item.id" :disabled="(!switchForm[index]||checkboxForm.length>=5)&&checkboxForm.indexOf(item.id)===-1">{{ (checkboxForm.indexOf(item.id)===-1)?'是否必填':'必填' }}</el-checkbox>
        <el-collapse-transition>
          <div v-if="switchForm[index]&&item.id===3" class="fields-tips">
            <div>选择项（单选）</div>
            <div>男/女/其它</div>
          </div>
        </el-collapse-transition>
        <el-collapse-transition>
          <div v-if="switchForm[index]&&item.id===11" class="fields-tips">
            <div>选择项（多选）</div>
            <div>游戏 / 阅读 / 音乐 / 运动 / 动漫 / 旅行 / 家居 / 曲艺 / 宠物/ 美食 / 娱乐 / 电影 / 电视剧 / 健康养生 / 数码 / 其他</div>
          </div>
        </el-collapse-transition>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="hide">取 消</el-button>
      <el-button size="small" round type="primary" @click="handleSubmit('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    fields: {
      require: true,
      type: Array
    },
    requiredForm: {
      require: true,
      type: Array
    },
    optionalForm: {
      require: true,
      type: Array
    }
  },
  data() {
    return {
      rules: {},
      // 配置
      resolve: null,
      reject: null,
      // 状态
      loading: false,
      dialogVisible: null,
      // 数据
      checkboxForm: [], // 必填状态
      switchForm: [] // 单选开启状态
    }
  },
  watch: {
    switchForm(val) {
      // 单选false时候 去除checkboxForm的id
      this.fields.forEach((item, index) => {
        const result = this.checkboxForm.indexOf(item.id)
        if (!this.switchForm[index] && result !== -1) {
          this.checkboxForm.splice(result, 1)
        }
      })
    },
    checkboxForm(val) {}
  },
  methods: {
    open() {
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
      this.checkboxForm = this.requiredForm
      const allSwitchId = this.requiredForm.concat(this.optionalForm)
      this.fields.forEach((val, index) => {
        if (allSwitchId.indexOf(val.id) !== -1) {
          this.switchForm[index] = true
        } else {
          this.switchForm[index] = false
        }
      })
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    handleSubmit(formName) {
      const optionalForm = []
      // 同步数据
      this.fields.forEach((item, index) => {
        const result = this.checkboxForm.indexOf(item.id)
        if (this.switchForm[index] && result === -1) {
          optionalForm.push(item.id)
        }
      })
      this.resolve &&
          this.resolve({
            requiredForm: this.checkboxForm,
            optionalForm: optionalForm
          })
      this.hide()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .fields-tips {
    font-size: 14px;
    color: $light-font-color;
    line-height: 1.15;
  }
</style>
